package oop.ex1.orders;
//###############
//FILE : NameFilter.java
//WRITER : Amir Wilf, wilfoa, 300007085
//WRITER : Daniel Acherkan, danithelion, 310116348
//EXERCISE : oop ex1 2011
//DESCRIPTION:
//A comparator to compare files lexicographically by their name
//###############
import java.io.File;
import java.io.IOException;
import java.util.Comparator;

public class FileOrder implements Comparator<File>{
	
	/**
	 * the operating system file separator sign
	 */
	final protected String SLASH = System.getProperty("file.separator");

	@Override
	public int compare(File file1, File file2) {
		//checking for equality and thus, use absolute path compare
		if(getFileName(file1).compareTo(getFileName(file2)) == 0){
			return file1.compareTo(file2);
		}
		//if they are not equal
		return getFileName(file1).compareTo(getFileName(file2));
	}

	//This method gets the file name
	private String getFileName(File file1) {
		try {
			String[] path = file1.getCanonicalPath().split(SLASH);
			return path[path.length-1];
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
